package com.djsearch.controller;

import com.djsearch.util.MD5;
import com.loopj.android.http.RequestParams;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Leo on 13-5-26.
 */
public class TopRequest extends RequestParams implements IRequest {
    Map<String,Object> map = new HashMap<String, Object>();
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public TopRequest(String method){
        this.map.put("method",method);
        this.put("method",method);
        //格式为yyyy-mm-dd HH:mm:ss
        String time = format.format(new Date());
        this.map.put("timestamp",time);
        this.put("timestamp",time);
        this.map.put("format","json");
        this.put("format","json");
        this.map.put("app_key","21378911");
        this.put("app_key","21378911");
        this.map.put("v","2.0");
        this.put("v", "2.0");
        this.map.put("sign_method","md5");
        this.put("sign_method","md5");
    }
    @Override
    public String url() {
        return "http://gw.api.tbsandbox.com/router/rest";
    }

    @Override
    public void putParam(String key, Object value) {
        map.put(key,value);
        this.put(key,value.toString());
    }

    @Override
    public String toParams() {
        Set<String> sortedKeys = new TreeSet<String>();
        sortedKeys.addAll(map.keySet());
        StringBuilder sb = new StringBuilder();
        sb.append("39bab81eeaea2b242e3cad5ecce6d653");
        for (String key:sortedKeys){
            sb.append(key+map.get(key).toString());
        }
        sb.append("39bab81eeaea2b242e3cad5ecce6d653");
        this.put("sign",MD5.getMD5(sb.toString()).toUpperCase());

        return this.toString();
    }
}
